//定义个当前分组的全局变量
var currentGroup = 'Ping';
//查询当前用户不同分组下的任务
function getListByGrop(controller) {
    $.ajax({
        type: "GET",
        cache:false,
        url: "/quartz/getListByGroup?jobGroup="+currentGroup + "&no_cache=" + new Date(),
        dataType: "json",
        success:function (data) {
            //先清空panel容器中的元素
            $("#panel").empty();
            //先将字符串转为对象
            $.each(data,function (key,value) {
                var itemList = " <div class=\"weui_cell weui_cell_switch\" th:each=\"serverMap:${serverMap}\">\n" +
                    "                    <div class=\"weui_cell_hd weui_cell_primary\" >" + key + "</div>\n" +
                    "                    <a href=" + controller + '?groupName='+currentGroup + key +"><i class=\"iconfont icon-xiugai2\" ></i></a>\n" +
                    "                    <div class=\"weui_cell_ft\">\n" +
                    "                       <input class=\"weui_switch\" name=\"isOpen\" onclick=runSwitch(this,"+key+") data-id='"+key+"'" + value + " type=\"checkbox\">\n" +
                    "                    </div>\n" +
                    "                </div>"
                //往panel容器中添加元素
                $("#panel").append(itemList);
            });
            //给后面添加的元素添加点击事件监听
            // $('input').click(function(){
            //    runSwitch($(this))
            // })
        }

    })
}
//文档加载完运行的函数
$(function(){
    //导航栏的点击事件
    $('.weui-navbar__item').on('click', function () {
        currentGroup = $(this).attr('data-group')
        var controller;
        $(this).addClass('weui-bar__item_on').siblings('.weui-bar__item_on').removeClass('weui-bar__item_on');
        if (currentGroup == 'Ping') {
            controller = '/openServerEditPage'
        } else if (currentGroup == 'Get') {
            controller = '/openStationEditPage'
        } else {
            controller = '/icp/openIcpEditPage'
        }
        getListByGrop(controller);
    });
    //开关点击事件
    // $('input').click(function(){
    //     runSwitch($(this))
    // })
    //任务运行开关
    $('#panel').on('click','.weui_switch',function () {
        groupName = currentGroup + $(this).attr('data-id')
        if($(this).attr("checked") == undefined) {
            $(this).attr("checked","checked");
            uri = '/quartz/turnOnTask?groupName=' + groupName + "&no_cache=" + new Date()
            $.ajax({
                type:'GET',
                cache:false,
                url: uri
            })
        } else {
            $(this).removeAttr("checked");
            uri = '/quartz/turnDownTask?groupName=' + groupName +"&no_cache=" + new Date()
            $.ajax({
                type:'GET',
                cache:false,
                url: uri
            })
        }
    })
});

